package com.example.test4;

import org.apache.http.Header;

import android.common.Models.Persona;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;

/**
 * A fragment representing a single Persona detail screen. This fragment is
 * either contained in a {@link PersonaListActivity} in two-pane mode (on
 * tablets) or a {@link PersonaDetailActivity} on handsets.
 */
public class PersonaDetailFragment extends Fragment {

	private Persona persona;

	/**
	 * Mandatory empty constructor for the fragment manager to instantiate the
	 * fragment (e.g. upon screen orientation changes).
	 */
	public PersonaDetailFragment() {
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		if (getArguments().containsKey("persona")) {
			persona = (Persona) getArguments().getSerializable("persona");
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		final View rootView = inflater.inflate(
				R.layout.fragment_persona_detail, container, false);

		// Show the dummy content as text in a TextView.
		if (persona != null) {
			((TextView) rootView.findViewById(R.id.persona_detail))
					.setText(persona.getDocumento());

			// persona = JSON.std.listOfFrom(Persona.class, personaString)
			// .get(0);

			EditText nombrePersona = (EditText) rootView
					.findViewById(R.id.textPersonName);
			EditText direccion = (EditText) rootView
					.findViewById(R.id.textAddress);
			EditText mail = (EditText) rootView.findViewById(R.id.textMail);
			EditText telefono = (EditText) rootView
					.findViewById(R.id.textPhone);
			nombrePersona.setText(persona.getNombres() + " "
					+ persona.getApellidos());
			direccion.setText(persona.getDireccion());
			telefono.setText(persona.getTelefono());
			mail.setText(persona.getEmail());
			// URL url = new URL(
			// "https://scontent-a-mia.xx.fbcdn.net/hphotos-xpf1/t1.0-9/10369742_10152035299921735_7173061531513648009_n.jpg");
			final ImageView imagen = (ImageView) rootView
					.findViewById(R.id.imagePersonPicture);

			AsyncHttpClient client = new AsyncHttpClient();
			client.get(
					"https://scontent-a-mia.xx.fbcdn.net/hphotos-xpf1/t1.0-9/10369742_10152035299921735_7173061531513648009_n.jpg",
					new AsyncHttpResponseHandler() {

						@Override
						public void onSuccess(int arg0, Header[] arg1,
								byte[] data) {
							Bitmap bitmap = BitmapFactory.decodeByteArray(data,
									0, data.length);
							imagen.setImageBitmap(bitmap);

						}

						@Override
						public void onFailure(int arg0, Header[] arg1,
								byte[] arg2, Throwable arg3) {
							imagen.setImageResource(R.drawable.ic_launcher);
						}
					});

			// Bitmap bitmap =
			// BitmapFactory.decodeStream(url.openConnection()
			// .getInputStream());

		}

		return rootView;
	}
}
